package com.taobao.ic.ticket.media;
import javax.media.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class PlayerMusic implements ControllerListener {// ControllerListener
              // 控制事件
 private Player player;

 private List<String> mp3List;
 private int mp3NO = 0;
 
 PlayerMusic(List<String> mp3List) {
  this.mp3List = mp3List;
 }

 public void start() {
  try {
   player = Manager.createPlayer(new MediaLocator("file://" + mp3List.get(mp3NO)));
  } catch (NoPlayerException ex) {
   ex.printStackTrace();
   System.out.println("不能播放文件");
   return;
  } catch (IOException ex) {
   ex.printStackTrace();
   return;
  }
  if (player == null) {
   System.out.println("播放器为空");
   return;
  }

  player.addControllerListener(this);
  // 提取媒体内容
  player.prefetch();

 }

 

 public void controllerUpdate(ControllerEvent e) {
  // 当媒体播放结束时,循环播放
  if (e instanceof EndOfMediaEvent) {
   mp3NO++;
   if(mp3NO<this.mp3List.size()){
    this.start();
   }
   return;
  }

  // 当预提取媒体的内容结束
  if (e instanceof PrefetchCompleteEvent) {
   player.start();
   return;
  }
  // 当实例化后
  if (e instanceof RealizeCompleteEvent) {
   // pack(); //执行pack()操作
   return;
  }

 }
 public static void main(String[] args) {
  List<String> mp3List = new ArrayList<String>();
  mp3List.add("D:\\音乐\\中国好声音\\郑虹 - Someone Like You Live.mp3");
//  mp3List.add("d://b.mp3");
//  mp3List.add("d://c.mp3");
  PlayerMusic pm = new PlayerMusic(mp3List);
  pm.start();
 }
}